package com.shb.game;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

public class MemNumActivity extends BaseActivity implements OnItemClickListener {

	private Button btnHelp;
	private Button btnBegin;
	private GridView gridMain;
	private TextView tvNum;
	/** 适配器 */
	private BaseAdapter adapter;
	/** 当前关 */
	private int level = 0;
	/** 当前数列 */
	private final List<Integer> lstNow = new ArrayList();
	/** 要显示的数组 */
	private final NumItem[] iArr = new NumItem[25];
	/** 个数 */
	private int num;
	/** 可视 */
	private boolean vis = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mem_num);
		level = getSharedPreferences("config", Context.MODE_PRIVATE).getInt("memLevel", 1);
		btnHelp = (Button) findViewById(R.id.btnHelp);
		btnBegin = (Button) findViewById(R.id.btnBegin);
		gridMain = (GridView) findViewById(R.id.gridMain);
		tvNum = (TextView) findViewById(R.id.tvNum);
		tvNum.setText(String.format("当前等级：%s", level));
		adapter = new ArrayAdapter<NumItem>(this, 0, iArr) {
			@Override
			public View getView(int paramInt, View paramView, ViewGroup paramViewGroup) {
				TextView btn = new TextView(MemNumActivity.this);
				btn.setWidth(40);
				btn.setHeight(40);
				btn.setGravity(Gravity.CENTER);
				if (iArr[paramInt] != null) {
					if (iArr[paramInt].value != 0) {
						btn.setTextColor(Color.BLACK);
						btn.setGravity(Gravity.CENTER);
						btn.setBackgroundResource(R.drawable.btn_bg);
						if (iArr[paramInt].bIsShow) {
							btn.setText(iArr[paramInt].value + "");
						}
					}
				}
				return btn;
			}

		};
		gridMain.setAdapter(adapter);
		gridMain.setOnItemClickListener(this);
		btnHelp.setOnClickListener(this);
		btnBegin.setOnClickListener(this);
		initDate();
	}

	private void createArr() {
		for (int i = 0; i < 25; i++) {
			iArr[i] = new NumItem();
		}
	}

	/**
	 * 初始化数据
	 */
	private void initDate() {
		lstNow.clear();
		createArr();
		num = level < 4 ? 3 : level < 8 ? 4 : (level + 17) / 5 > 25 ? 25 : (level + 17) / 5;
		List<Integer> lst = new ArrayList();
		for (int i = 1; i < 26; i++) {
			lst.add(i);
		}
		for (int i = 0; i < num; i++) {
			int row = new Random().nextInt(5);
			int col = new Random().nextInt(5);
			while (iArr[row * 5 + col].value != 0) {
				row = new Random().nextInt(5);
				col = new Random().nextInt(5);
			}
			int k = new Random().nextInt(lst.size());
			iArr[row * 5 + col].value = lst.get(k);
			lstNow.add(lst.get(k));
			lst.remove(k);
		}
		Collections.sort(lstNow);
		adapter.notifyDataSetChanged();
		vis = true;
	}

	@Override
	public void onClick(View paramView) {
		switch (paramView.getId()) {
		case R.id.btnHelp:
			startActivity(new Intent(this, MemNumHelpActivity.class));
			break;
		case R.id.btnBegin:
			if (vis) {
				for (int i = 0; i < iArr.length; i++) {
					if (iArr[i].value > 0) {
						iArr[i].bIsShow = true;
					}
				}
				adapter.notifyDataSetChanged();
				new Handler().postDelayed(new Runnable() {

					@Override
					public void run() {
						for (int i = 0; i < iArr.length; i++) {
							iArr[i].bIsShow = false;
						}
						adapter.notifyDataSetChanged();
					}
				}, num * 400);
				vis = false;
			}

			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> paramAdapterView, View paramView, int paramInt, long paramLong) {
		if (!vis) {
			NumItem ni = iArr[paramInt];
			if (ni.bIsShow) {
				return;
			}
			ni.bIsShow = true;
			adapter.notifyDataSetChanged();
			if (ni.value != 0) {
				if (ni.value == lstNow.get(0)) {
					lstNow.remove(0);
					if (lstNow.isEmpty()) {
						Toast.makeText(this, "恭喜你完成了,进入下一级！", 0).show();
						level++;
						tvNum.setText(String.format("当前等级：%s", level));
						getSharedPreferences("config", Context.MODE_PRIVATE).edit().putInt("memLevel", level).commit();
						initDate();
					}
				} else {
					Toast.makeText(this, "很可惜，选错了", 0).show();
					initDate();
				}
			}
		}
	}

	/***
	 * 要显示的数字对象
	 * 
	 * @name NumItem
	 * @author greenery
	 * @date 2012-4-6
	 */
	class NumItem {
		public int value = 0;
		public boolean bIsShow = false;
	}

}
